package com.fary.security.config.annotation.web.configuration;

import com.fary.context.annotation.ImportSelector;
import com.fary.core.type.AnnotationMetadata;
import com.fary.util.ClassUtils;

import java.util.LinkedHashSet;
import java.util.Set;

final class OAuth2ImportSelector implements ImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        Set<String> imports = new LinkedHashSet<>();

        boolean oauth2ClientPresent = ClassUtils.isPresent(
                "org.springframework.security.oauth2.client.registration.ClientRegistration", getClass().getClassLoader());
        if (oauth2ClientPresent) {
            imports.add("org.springframework.security.config.annotation.web.configuration.OAuth2ClientConfiguration");
        }

        boolean webfluxPresent = ClassUtils.isPresent(
                "org.springframework.web.reactive.function.client.ExchangeFilterFunction", getClass().getClassLoader());
        if (webfluxPresent && oauth2ClientPresent) {
            imports.add("org.springframework.security.config.annotation.web.configuration.SecurityReactorContextConfiguration");
        }

        boolean oauth2ResourceServerPresent = ClassUtils.isPresent(
                "org.springframework.security.oauth2.server.resource.BearerTokenError", getClass().getClassLoader());
        if (webfluxPresent && oauth2ResourceServerPresent) {
            imports.add("org.springframework.security.config.annotation.web.configuration.SecurityReactorContextConfiguration");
        }

        return imports.toArray(new String[0]);
    }
}